﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ITunesSolver.Helpers;
using System.IO;
using System.Collections.ObjectModel;
using ITunesSolver.Helpers.DataVirtualization;
using System.Threading;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Threading;

namespace ITunesSolver.UI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //List<string> keys = ITunesHelper.GetITunesTrackKeys(@"C:\Users\hstirling\Documents\BENNEVIS iTunes Music Library.xml");
            //List<string> keys = ITunesHelper.GetITunesPlaylistKeys(@"C:\Users\hstirling\Documents\BENNEVIS iTunes Music Library.xml");

            //File.WriteAllLines("c:\\temp\\keys.txt", keys.ToArray());

            //ITunesHelper.LoadITunesLibrary(@"C:\Users\hstirling\Documents\Original_iTunesMusicLibrary.xml");
            //ITunesHelper.SaveITunesLibrary(@"c:\temp\itunestest.xml");

            //InitializeControls();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
        }

        private void InitializeControls()
        {
            Trace.WriteLine("InitializeControls START");
            InitializeMetaDataTab();
            InitializeSongTab();
            InitializePlaylistTab();
            Trace.WriteLine("InitializeControls DONE");
        }

        private void InitializeSongTab()
        {
            if (ITunesHelper.Songs != null)
            {
                lvTracks.Initialize(ITunesHelper.Songs);
            }
        }

        private void InitializePlaylistTab()
        {
            if (ITunesHelper.Playlists != null)
            {
                lvPlaylists.Initialize(ITunesHelper.Playlists);
            }
        }

        private void InitializeMetaDataTab()
        {
            if (ITunesHelper.MetaData != null)
            {
                tbApplicationVersion.Text = ITunesHelper.MetaData.ApplicationVersion;
                tbFeatures.Text = ITunesHelper.MetaData.Features.ToString();
                tbLibraryPersistentId.Text = ITunesHelper.MetaData.LibraryPersistentId;
                tbMajorVersion.Text = ITunesHelper.MetaData.MajorVersion.ToString();
                tbMinorVersion.Text = ITunesHelper.MetaData.MinorVersion.ToString();
                tbMusicFolder.Text = ITunesHelper.MetaData.MusicFolder;
                chkShowContentRating.IsChecked = ITunesHelper.MetaData.ShowContentRatings;
            }
        }

        private void miFile_Load_Click(object sender, RoutedEventArgs e)
        {
            Trace.WriteLine("miFile_Load_Click START");

            LoadFilesForm loadFilesForm = new LoadFilesForm();
            loadFilesForm.Owner = this;
            loadFilesForm.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            loadFilesForm.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            loadFilesForm.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
            loadFilesForm.ShowDialog();

            //string fileName = string.Empty;
            //fileName = FileHelper.SelectFile("iTunes Music Library.xml");

            //Thread thread = new Thread(LoadITunesLibrary);
            //thread.SetApartmentState(ApartmentState.MTA);
            //thread.Start(fileName);

            //old stuff
            //bool result = ITunesHelper.LoadITunesLibrary(fileName);

            //InitializeControls();

            //lvTracks.UpdateLayout();
            //lvPlaylists.UpdateLayout();

            Trace.WriteLine("miFile_Load_Click DONE");
        }

        private delegate void DummyDelegate();

        private void LoadITunesLibrary(object fileName)
        {
            Trace.WriteLine("LoadITunesLibrary START");

            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            worker.RunWorkerAsync(fileName);

            //System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((DummyDelegate)delegate { ITunesHelper.LoadITunesLibrary(fileName.ToString()); }, null);

            //System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((DummyDelegate)delegate { InvokeInitializeControls(); }, null);
            //System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((DummyDelegate)delegate { InvokeTracksLayout(); }, null);
            //System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((DummyDelegate)delegate { InvokePlaylistsLayout(); }, null);

            Trace.WriteLine("LoadITunesLibrary DONE");
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            BackgroundWorker worker1 = new BackgroundWorker();
            worker1.DoWork += new DoWorkEventHandler(worker1_DoWork);
            worker1.RunWorkerAsync();

            BackgroundWorker worker2 = new BackgroundWorker();
            worker2.DoWork += new DoWorkEventHandler(worker2_DoWork);
            worker2.RunWorkerAsync();

            BackgroundWorker worker3 = new BackgroundWorker();
            worker3.DoWork += new DoWorkEventHandler(worker3_DoWork);
            worker3.RunWorkerAsync();

            //System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((DummyDelegate)delegate { InvokeInitializeControls(); }, null);
            //System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((DummyDelegate)delegate { InvokeTracksLayout(); }, null);
            //System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((DummyDelegate)delegate { InvokePlaylistsLayout(); }, null);
        }

        void worker3_DoWork(object sender, DoWorkEventArgs e)
        {
            InvokePlaylistsLayout();
        }

        void worker2_DoWork(object sender, DoWorkEventArgs e)
        {
            InvokeTracksLayout();
        }

        void worker1_DoWork(object sender, DoWorkEventArgs e)
        {
            InvokeInitializeControls();
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            ITunesHelper.LoadITunesLibrary(e.Argument.ToString());
        }

        private void InvokeInitializeControls()
        {
            Trace.WriteLine("InvokeInitializeControls START");
            Dispatcher.BeginInvoke(new DummyDelegate(InitializeControls), DispatcherPriority.Background);
            Trace.WriteLine("InvokeInitializeControls DONE");
        }

        private void InvokeTracksLayout()
        {
            Trace.WriteLine("InvokeTracksLayout START");
            Dispatcher.BeginInvoke(new DummyDelegate(lvTracks.UpdateLayout), DispatcherPriority.Background);
            Trace.WriteLine("InvokeTracksLayout DONE");
        }
        private void InvokePlaylistsLayout()
        {
            Trace.WriteLine("InvokePlaylistsLayout START");
            Dispatcher.BeginInvoke(new DummyDelegate(lvPlaylists.UpdateLayout), DispatcherPriority.Background);
            Trace.WriteLine("InvokePlaylistsLayout DONE");
        }
    }


}
